package it.ma.musicplayernew.helper;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zhongdao.swing.R;


public class ImageLoaderHelper {
    private static DisplayImageOptions baseDisplayImageOptions;
    private static ImageLoader imageLoader;

    /**
     * 显示一个圆角图片
     * @param url 图片网络uri
     * @param imageView
     * @param rpx 圆角像素值
     */
    public static void displayRoundImage(String url, ImageView imageView, int rpx) {
        DisplayImageOptions options = getRoundedDisplayImageOptions(rpx, R.mipmap.default_artist);
        getImageLoader().displayImage(url, imageView, options);

    }

    /**
     * 显示一个圆角图片
     * @param url 图片网络uri
     * @param imageView
     * @param rpx 圆角像素值
     */
    public static void displayRoundImage(String url, ImageView imageView, int rpx,int defaultResId) {
        DisplayImageOptions options = getRoundedDisplayImageOptions(rpx,defaultResId);
        getImageLoader().displayImage(url, imageView, options);

    }

    public static void displayImage(String url, ImageView imageView) {
    	getImageLoader().displayImage(url, imageView, getBaseDisplayImageOptions());
    }

    public static void displayImage(String url, ImageView imageView,int defaultResId) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//图像将完全按比例缩小的目标大小
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .displayer(new SimpleBitmapDisplayer())
                .showImageOnLoading(defaultResId)
                .showImageForEmptyUri(defaultResId)
                .showImageOnFail(defaultResId)
                .build();
        getImageLoader().displayImage(url, imageView, options);
    }
    
    public static void displayImage(String url, ImageView imageView,ImageLoadingListener listener) {
    	getImageLoader().displayImage(url, imageView, getBaseDisplayImageOptions(),listener);
    }
    
    public static void displayImage(ImageLoader loader,String url, ImageView imageView,ImageLoadingListener listener) {
    	loader.displayImage(url, imageView, getBaseDisplayImageOptions(),listener);
    }

    public static DisplayImageOptions getBaseDisplayImageOptions() {
        if (baseDisplayImageOptions == null) {
            baseDisplayImageOptions = new DisplayImageOptions.Builder()
                                    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//图像将完全按比例缩小的目标大小
            						.cacheInMemory(true)
            						.cacheOnDisk(true)
            						.displayer(new SimpleBitmapDisplayer())
            						.build();
        }
        return baseDisplayImageOptions;
    }

    public static DisplayImageOptions getRoundedDisplayImageOptions(int rpx,int defaultResId) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .imageScaleType(ImageScaleType.EXACTLY)//图像将完全按比例缩小的目标大小
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageForEmptyUri(defaultResId)  // empty URI时显示的图片
                .showImageOnFail(defaultResId)       // 不是图片文件 显示图片
                .showImageOnLoading(defaultResId)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .displayer(new RoundedBitmapDisplayer(rpx))
                .build();
        return options;
    }


    public static ImageLoader getImageLoader() {
        if (imageLoader == null) {
            imageLoader = ImageLoader.getInstance();
        }
        return imageLoader;
    }

    public static String translateLoaclImageDirFormat(String path) {
        String s = "file://" + path;
        return s;
    }

    public static String translateResouceFormat(int resouceId) {
        String s = "drawable://" + resouceId;

        return s;
    }
}
